package web

import (
	"net/url"
	"strings"
)

func ExtractDomain(urlString string) (string, error) {
	var domain string
	u, err := url.Parse(urlString)
	if err != nil {
		return "", err
	}
	stringSlice := strings.Split(u.Host, ".")
	if len(stringSlice) == 2 {
		domain = stringSlice[0] + "." + stringSlice[1]
	} else if len(stringSlice) > 2 {
		domain = stringSlice[len(stringSlice)-2] + "." + stringSlice[len(stringSlice)-1]
	} else {
		domain = ""
	}
	return domain, nil
}
